package com.cheyun.netsalev3.view.defeatdetails

import android.util.Log
import androidx.databinding.library.baseAdapters.BR
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.CustomerEditTagAdapter
import com.cheyun.netsalev3.adapter.HomePageAdapter
import com.cheyun.netsalev3.bean.Data
import com.cheyun.netsalev3.databinding.ActivityDefeatDetailsBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.ClueDetailsActivityViewModel
import com.jeremyliao.liveeventbus.LiveEventBus


/**
 * 线索详情
 */
class DefeatDetailsActivity : BaseActivity<ActivityDefeatDetailsBinding>() {
    var tid = 0
    var revkey = ""
    var nkey = ""
    var come = ""
    var customerTagAdapter: CustomerEditTagAdapter? = null

    override fun initContentView(): Int {
        return R.layout.activity_defeat_details
    }

    override fun initViewModel(): ViewModel? {
        if(intent.hasExtra("tid")) {
            tid = intent.getIntExtra("tid", 0)
            if (tid == 0) {
                var t = intent.getStringExtra("tid")
                if(!t.isNullOrEmpty())
                    tid = intent.getStringExtra("tid").toInt()
            }
        }
        if(intent.hasExtra("revkey")) {
            revkey = intent.getStringExtra("revkey")
        }
        if(intent.hasExtra("nkey")) {
            nkey = intent.getStringExtra("nkey")
        }

        if(intent.hasExtra("come"))
            come = intent.getStringExtra("come")
//        跟进：archive.purge 1
//        订车：archive.order 5
//        提车：archive.thenend 10
//        战败：archivecheck.fail 32
//        titleList = when (nkey) {
//            "archivecheck.fail" ->
//                arrayListOf("战败详情", "历史记录","语音分析")
//            "archivecheck.invalid" ->
//                arrayListOf("无效详情", "历史记录","语音分析")
//            else -> arrayListOf("线索详情", "历史记录","语音分析")
//
//        }
        titleList = arrayListOf("跟进记录", "任务列表","意向单","销售订单")
        var keliudata:Data? = null
        if(intent.hasExtra("keliudata")) {
            keliudata = (intent.getSerializableExtra("keliudata") as Data)
        }
        val factory = InjectorUtils.getClueDetailsViewModelFactory(tid, nkey,come,keliudata,revkey)
        return ViewModelProviders.of(this, factory).get(ClueDetailsActivityViewModel::class.java)
    }

    lateinit var titleList: ArrayList<String>
    override fun initView() {
        var list = ArrayList<Fragment>()
        list.add(HistoricalRecordFragment())
        list.add(RecordVoiceFragment())
        list.add(RecordVoiceFragment())
        list.add(RecordVoiceFragment())
        var adapter = HomePageAdapter(supportFragmentManager, list)
        adapter.titles = titleList
        binding.viewPager.adapter = adapter
        binding.tabLayout.setSelectedTabIndicator(R.drawable.tab_indicator)
        binding.tabLayout.setupWithViewPager(binding.viewPager)


        customerTagAdapter = CustomerEditTagAdapter(this,null,false)
        binding.customerTagFlow.setAdapter(customerTagAdapter)

        LiveEventBus.get()
                .with("clueupdatge", String::class.java)
                .observe(this, Observer {
                    Log.d("club detail","change")
                    binding.viewModel?.refreshDetail()
                })
    }

    override fun initData() {
        binding.viewModel?.clueRow?.observe(this, Observer { list ->
            binding.clueItemView.setData(list)
        })
        binding.viewModel?.userTag?.observe(this, Observer {
            customerTagAdapter?.setData(it)
        })
    }


}
